package cn.jsu.oj.dispatchServer.init;

import cn.jsu.oj.dispatchServer.dataStructure.MappingQueue;
import cn.jsu.oj.dispatchServer.parsing.ClientInitType;
import cn.jsu.oj.dispatchServer.pojo.TaskInfo;
import cn.jsu.oj.dispatchServer.resource.Resource;
import cn.jsu.oj.dispatchServer.thread.DispatchThread;
import cn.jsu.oj.dispatchServer.utils.NetUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 24987
 */
@Slf4j
@ClientInitType(type = 1)
public class JudgeClient extends AbstractClientInit {

    private MappingQueue<TaskInfo> mappingQueue;

    private Map<Long, TaskInfo> storeMap;

    public MappingQueue<TaskInfo> getMappingQueue() {
        return mappingQueue;
    }

    public void setMappingQueue(MappingQueue<TaskInfo> mappingQueue) {
        this.mappingQueue = mappingQueue;
    }

    public Map<Long, TaskInfo> getStoreMap() {
        return storeMap;
    }

    public void setStoreMap(Map<Long, TaskInfo> storeMap) {
        this.storeMap = storeMap;
    }

    @Override
    public void close() {
        //将任务返回到任务大队列
        super.close();
        mappingQueue.forEach(it->{
            log.error("判题机映射队列中的任务回到大队列"+":任务Id-"+it);
        });
        Resource.taskBigQueue.addAll(mappingQueue);
        //将暂存任务(已经发送但是没有返回结果)返回任务大队列
        Resource.storeTaskMap.remove(NetUtil.getIp(channel));
        for (Long s : storeMap.keySet()) {
            Resource.taskBigQueue.add(storeMap.get(s));
        }
        log.info("判题机映射队列返回到任务大队列成功");
        storeMap.clear();
        //删除映射队列
        DispatchThread.judgeMap.remove(this);
        //删除映射队列
//        DispatchThread.judgeMap.remove(mappingQueue);


    }



    public JudgeClient(int initSize) {
        mappingQueue = new MappingQueue<>(initSize);
        storeMap = new ConcurrentHashMap<>();
    }
}
